package com.fusion.launchers;

import com.fusion.Version;
import com.fusion.core.ResourceSingelton;

import java.awt.*;

/**
 * @author SwethaShivakumar Project: fusion Date: Mar 16, 2009 URL:
 *         http://fusionwiki.co.cc Mail Me At: shiva@fusionproject.co.cc
 */
public class SplashWindow extends Window implements Runnable,
		StartupProgressListener {
	private static final long serialVersionUID = 1L;
	private Image image;
	private String statusMessage = "";
	private int progressPercent = -1;
	private String versionStr;

	public SplashWindow() throws Exception {
		super(new Frame());
		image = Toolkit.getDefaultToolkit().getImage(
				ResourceSingelton.getRl().getResource("gfx/splash.jpg"));
		MediaTracker mt = new MediaTracker(SplashWindow.this);
		mt.addImage(image, 0);
		try {
			mt.waitForAll();
		} catch (InterruptedException e) {
		}
		Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();

		setLocation(ss.width / 2 - image.getWidth(null) / 2, ss.height / 2
				- image.getHeight(null) / 2);
		setSize(new Dimension(image.getWidth(null), image.getHeight(null)));
		versionStr = "Version " + Version.VERSION + " " + Version.BUILD_NUMBER
				+ " Byilt By: Shiva";
		setVisible(true);
		toFront();
		requestFocus();
	}

	private int progressBarLength = 100, progressBarHeight = 8;
	/**
	 * By Shiva
	 * 
	 * @2:52 AM 3/27/2009 For Showing ProgressBar on Splash screen, It is used
	 *       by com.fusion.launchers.SplashWindow class to update progress bar
	 *       drawn percent. To update progress percent use
	 *       CoreSubsystem.setStartupProgressPercent(int percent) then call
	 *       SplasWindow.updateProgress(String Msg) to draw(show) current
	 *       progress percent in SplashScreen.
	 */
	private static int startupProgressPercent = 0;
	
	// paint Method is Modified To Display Progress bar at Bottom of Splash screen By
	// Shiva @3:19 AM 3/27/2009
	public void paint(Graphics frontG) {
		Graphics2D g = (Graphics2D) frontG;

		g.drawImage(image, 0, 0, null);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g.setFont(new Font("Arial Black, Arial", 0, 10));
		g.setColor(Color.white);
//		@3:23 AM 3/27/2009 by Shiva
		int texty = image.getHeight(null) - progressBarHeight - 4; // = image.getHeight(null) - 10;
		g.drawString(statusMessage, 10, texty);
		String s = versionStr;
		g.drawString(s, image.getWidth(null) - 10
				- g.getFontMetrics().stringWidth(s), texty);

		if (progressPercent >= 0) {
//			Alpha increased to 5 times/2 from 3times/2 for rapid increase 
//			and max alpha increased to 250 from 70 @4:34 AM 3/27/2009 by Shiva
			int a = progressPercent * 5 / 2; //Progress bar Color's Alpa value
			if (a > 250) a = 250;
			g.setColor(new Color(255, 255, 255, a));// ));//
			progressBarLength = image.getWidth(null)-2;// Increased bar length to full length  - 15 * 4;
//			progressBarHeight = 20; //decreased bar height to initial value
			int x = image.getWidth(null) / 2 - progressBarLength / 2;

			int y = image.getHeight(null) - 1 - progressBarHeight; //- 51;
			
			g.drawRect(x, y, progressBarLength, progressBarHeight);
			g.fillRect(x + 2, y + 2, (progressBarLength - 3) * progressPercent
					/ 100, progressBarHeight - 3);
		}

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	public void update(Graphics g) {
		paint(g);
	}

	public void setStatusMessage(String statusMessage) {
		this.statusMessage = statusMessage;
		setProgressPercent(SplashWindow.getStartupProgressPercent());
		if (getGraphics() != null)
			paint(getGraphics());
	}

	public void setProgressPercent(int i) {
		progressPercent = i;
	}

	public void run() {
		setVisible(false);
		dispose();
	}

	public void updateProgress(String message) {
		setStatusMessage(message + "...");
	}

	public static void setStartupProgressPercent(int startupProgressPercent) {
		SplashWindow.startupProgressPercent = startupProgressPercent;
	}

	public static int getStartupProgressPercent() {
		return startupProgressPercent;
	}

}
